<template>
  <div class="customer-trackingplan-container">
    <top-tip :tipcontent="tipcontent" />
    <el-calendar v-model="queryForm.date" @click="handleDateClick">
      <template #date-cell="{ data }">
        <div>{{ data.day.split('-').slice(1).join('-') }}</div>
        <div style="margin-top: 5px">
          <div
            v-for="(item, index) in dealMyDate(data.day)"
            :key="index"
            style="font-size: 12px; margin-top: 5px"
            @click="getCustomerTrack(item)"
          >
            {{ item.CustomerAllName }}
          </div>
        </div>
      </template>
    </el-calendar>
  </div>
</template>

<script>
  import { getList, doDelete } from '@/api/customerTrackingplan'
  import TopTip from '@/components/TopTip'
  import { Delete, Plus, Search } from '@element-plus/icons-vue'

  export default defineComponent({
    name: 'CustomerTrackingplan',
    components: { TopTip },
    setup() {
      const router = useRouter()
      const $baseConfirm = inject('$baseConfirm')
      const $baseMessage = inject('$baseMessage')

      const state = reactive({
        tipcontent: {
          title: '我的客户跟进计划',
          memo: '',
        },
        editRef: null,
        list: [],
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        selectRows: '',
        queryForm: {
          pageNo: 1,
          pageSize: 10,
          title: '',
          date: new Date(),
        },
      })

      const dealMyDate = (v) => {
        let res = ''
        for (let i = 0; i < state.list.length; i++) {
          if (state.list[i].date == v) {
            res = state.list[i].content
          }
        }
        return res
      }

      const handleDateClick = () => {
        console.log('queryForm.date', state.queryForm.date)
      }

      const getCustomerTrack = (row) => {
        router.push({
          path: '/customer/track/trackingBusiness',
          query: { CustomerPickID: row.CustomerPickID },
        })
      }

      const setSelectRows = (val) => {
        state.selectRows = val
      }
      const handleEdit = (row) => {
        if (row.id) {
          state.editRef.showEdit(row)
        } else {
          state.editRef.showEdit()
        }
      }
      const handleDelete = (row) => {
        if (row.id) {
          $baseConfirm('你确定要删除当前项吗', null, async () => {
            const { msg } = await doDelete({ ids: row.id })
            $baseMessage(msg, 'success', 'vab-hey-message-success')
            await fetchData()
          })
        } else {
          if (state.selectRows.length > 0) {
            const ids = state.selectRows.map((item) => item.id).join()
            $baseConfirm('你确定要删除选中项吗', null, async () => {
              const { msg } = await doDelete({ ids })
              $baseMessage(msg, 'success', 'vab-hey-message-success')
              await fetchData()
            })
          } else {
            $baseMessage('未选中任何行', 'error', 'vab-hey-message-error')
          }
        }
      }
      const handleSizeChange = (val) => {
        state.queryForm.pageSize = val
        fetchData()
      }
      const handleCurrentChange = (val) => {
        state.queryForm.pageNo = val
        fetchData()
      }
      const queryData = () => {
        state.queryForm.pageNo = 1
        fetchData()
      }
      const fetchData = async () => {
        state.listLoading = true
        const {
          data: { list, total },
        } = await getList(state.queryForm)
        state.list = list
        state.total = total
        state.listLoading = false
      }
      onMounted(() => {
        fetchData()
      })

      return {
        ...toRefs(state),
        getCustomerTrack,
        handleDateClick,
        setSelectRows,
        handleEdit,
        handleDelete,
        handleSizeChange,
        handleCurrentChange,
        queryData,
        fetchData,
        Delete,
        Plus,
        Search,
        dealMyDate,
      }
    },
  })
</script>

<style>
  .el-calendar-table .el-calendar-day {
    box-sizing: border-box;
    min-height: var(--el-calendar-cell-width);
    height: auto;
    padding: 8px;
  }
</style>
